﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace RemoteGamePad
{
    public partial class MousePage : PhoneApplicationPage
    {
        double x;
        double y;
        TranslateTransform t;
        GamePadServiceReference.GamePadServiceClient gamePadService;

        public MousePage()
        {
            InitializeComponent();
            t = new TranslateTransform();
            //mouseControl.RenderTransform = t;
            SetupWCFService();
            x = 0;
            y = 0;
        }

        private void SetupWCFService()
        {
            gamePadService = new GamePadServiceReference.GamePadServiceClient();
            var settings = IsolatedStorageSettings.ApplicationSettings;
            String adress = "127.0.0.1";
            if (settings.Contains("adress"))
            {
                adress = settings["adress"].ToString();
            }
            gamePadService.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://" + "192.168.1.68" + ":59361/GamePadService.svc");

        }

        private void mouseControl_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            e.Handled = true;
            t.X += e.ManipulationOrigin.X;
            t.Y += e.ManipulationOrigin.Y;
        }

        private void mouseControl_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {

        }

        private void mouseControl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            e.Handled = true;
            try
            {
                gamePadService.TransmitMouseInfoAsync(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured.\n Details:" + ex.Message);
                gamePadService.CloseAsync();
            }
            t.X += e.DeltaManipulation.Translation.X;
            t.Y += e.DeltaManipulation.Translation.Y;
        }

        private void ContentPanel_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            //e.Handled = true;
            //t.X += e.ManipulationOrigin.X;
            //t.Y += e.ManipulationOrigin.Y;
        }

        private void init_click(object sender, RoutedEventArgs e)
        {
            t.X = 20;
            t.Y = 20;
        }

        private void mouseContainer_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            e.Handled = true;
            x = e.ManipulationOrigin.X;
            y = e.ManipulationOrigin.Y;
            textBlockX.Text = "X: " + e.ManipulationOrigin.X.ToString();
            textBlockY.Text = "Y: " + e.ManipulationOrigin.Y.ToString();

        }

        private void mouseContainer_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            e.Handled = true;
            x += e.DeltaManipulation.Translation.X;
            y += e.DeltaManipulation.Translation.Y;
            if (y >= 0 && y <= mouseContainer.Height && x >= 0 && x <= mouseContainer.Width)
            {
                textBlockX.Text = "X: " + x.ToString();
                textBlockY.Text = "Y: " + y.ToString();
                try
                {
                    gamePadService.TransmitMouseInfoAsync(x, y);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occured.\n Details:" + ex.Message);
                    gamePadService.CloseAsync();
                }
            }
            //System.Threading.Thread.Sleep(250);
        }
    }
}